package com.wqh.user.controller;

import com.wqh.user.entity.User;
import com.wqh.user.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wanqh
 * @date 2018/01/10 17:11
 * @description:
 */

@RestController
public class UserController {
    @Autowired
    private UserRepository userRepository;
    @GetMapping("/user/{id}")
    public User findById(@PathVariable("id") Long id) throws InterruptedException {
//        Thread.sleep(5000);
        return userRepository.findOne(id);
    }

    @PostMapping("/user/insert")
    public User insert(User user){
        return userRepository.save(user);
    }
    @PostMapping("/u/update")
    public User uodate(@RequestBody User user){
        System.out.println(user);
        return userRepository.save(user);
    }
    @GetMapping("/users/ids")
    public List<User> findUserByIds(String ids){
        System.out.println(">>>>>>>>>>"+ids);
        String[] split = ids.split(",");
        List<User> result = new ArrayList<>();
        for (String s : split){
            Long id = Long.valueOf(s);
            User  user = userRepository.findOne(id);
            result.add(user);
        }
        return result;
    }
}
